package com.dynamic.model.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.dynamic.model.common.dao.BaseDao;
import com.dynamic.model.common.vo.Page;
import com.dynamic.model.model.po.SysmanUser;
import com.dynamic.model.model.po.SystemCode;
import com.dynamic.model.util.redis.CacheManager;
import com.dynamic.model.dao.SystemCodeDaoI;
import com.dynamic.model.service.SystemCodeServiceI;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import com.dynamic.model.common.service.impl.BaseServiceImpl;

@Service
public class SystemCodeServiceImpl extends BaseServiceImpl<SystemCode> implements SystemCodeServiceI {

	@Override
	@Resource
	public void setBaseDao(BaseDao<SystemCode> dao) {
		this.baseDao =  dao;
	}
	
	public SystemCodeDaoI getDao(){
		return (SystemCodeDaoI) this.baseDao ;
	}
	
	@Override
	public List<SystemCode> getPageDataByName(Page<SystemCode> page, String key) {
		return this.getDao().getPageDataReqName(page, key);
	}
	
	@Override
	public void save(SystemCode entity) throws Exception {
		Subject subject = SecurityUtils.getSubject();
		SysmanUser user = (SysmanUser) subject.getPrincipal();
		entity.setCreater(user.getPid());
		this.getBaseDao().save(entity);
		CacheManager.getInstance().setAttributeObj(entity.getCodeKey(), entity.getCodeValue());
	}
	
	@Override
	public void update(SystemCode entity) throws Exception {
		Subject subject = SecurityUtils.getSubject();
		SysmanUser user = (SysmanUser) subject.getPrincipal();
		entity.setCreater(user.getPid());
		this.getBaseDao().update(entity);
		CacheManager.getInstance().setAttributeObj(entity.getCodeKey(), entity.getCodeValue());
	}
	
	@Override
	public void deleteLogicByIds(String[] pidArray) throws Exception {
		this.getDao().deleteLogicByIds(pidArray);
		for(String pid:pidArray){
			CacheManager.getInstance().removeAttribute(this.getBaseDao().get(Integer.parseInt(pid)).getCodeKey());
		}
	}

	@Override
	public SystemCode getCodeByKey(String key) {
		return this.getDao().getCodeByKey(key);
	}
}
